<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
			label-width="98px">
			<div class="app-space-between">
				<div class="app-space-between-left">

					<el-form-item label="会议室名称:" prop="name">
						<el-input v-model="queryParams.name" placeholder="请输入会议室名称" clearable @clear="handleQuery"
							@keyup.enter.native="handleQuery" />
					</el-form-item>
					<el-form-item label="所属单位:" prop="affiliatedUnit">
						<el-input v-model="queryParams.affiliatedUnit" placeholder="请输入所属单位" clearable
							@clear="handleQuery" @keyup.enter.native="handleQuery" />
					</el-form-item>

					<el-form-item label="状态:" prop="status">
						<el-select v-model="queryParams.status" @change="handleQuery" placeholder="请选择状态" clearable>
							<el-option v-for="dict in dict.type.hotel_statistics_status" :key="dict.value"
								:label="dict.label" :value="dict.value" />
						</el-select>
					</el-form-item>
				</div>
				<div class="app-space-between-right">
					<el-form-item>
						<el-button type="primary" size="mini" @click="handleQuery">搜索</el-button>
						<el-button size="mini" @click="resetQuery">重置</el-button>
					</el-form-item>
				</div>
			</div>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain size="mini" @click="handleAdd" v-hasPermi="['system:post:add']">新增
				</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="primary" plain size="mini" :disabled="single" @click="handleUpdate">编辑</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="primary" plain size="mini" :disabled="multiple" @click="handleDelete">删除</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="会议室名称" align="center" prop="name" />
			<el-table-column label="所属单位" align="center" prop="affiliatedUnit" />
			<el-table-column label="排序号" align="center" prop="sortNumber" />
			<el-table-column label="状态" align="center" prop="status">
				<template slot-scope="scope">
					<dict-tag :options="dict.type.hotel_statistics_status" :value="scope.row.status" />
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
			@pagination="getList" />

		<el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="100px" v-loading="diLogloading">
				<el-row :gutter="20">
					<el-col :span="24">
						<el-form-item label="会议室名称:" prop="name">
							<el-input v-model="form.name" placeholder="请输入会议室名称" />
						</el-form-item>
					</el-col>
				</el-row>

				<el-row :gutter="20">
					<el-col :span="24">
						<el-form-item label="所属单位:" prop="affiliatedUnit">
							<el-input v-model="form.affiliatedUnit" placeholder="请输入所属单位" />
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="24">
						<el-form-item label="会议室属性:" prop="goodsAttribute">
							<el-tag style="margin-right: 10px;" :key="index" v-for="(tag,index) in dynamicTags" closable
								:disable-transitions="false" @close="handleClose(tag)">
								{{tag}}
							</el-tag>
							<el-input class="input-new-tag" style="width:120px;" v-if="inputVisible"
								v-model="inputValue" ref="saveTagInput" size="small"
								@keyup.enter.native="handleInputConfirm" @blur="handleInputConfirm">
							</el-input>
							<el-button v-else class="button-new-tag" size="small"
								@click="showInput">+ 添加</el-button>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row :gutter="20">
					<el-col :span="24">
						<el-form-item label="会议室简介:" prop="content">
							<quill-edit v-model="form.content" :height="300"></quill-edit>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row :gutter="20">
					<el-col :span="12">
						<el-form-item label="会议室图片:" prop="picAddress">
							<image-upload :isShowTip="false" v-model="form.picAddress" :limit="1"></image-upload>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="轮播图:" prop="chartAddress">
							<image-upload :isShowTip="false" v-model="form.chartAddress" :limit="99"></image-upload>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row :gutter="20">
					<el-col :span="12">
						<el-form-item label="启用状态:" prop="status">
							<el-radio-group v-model="form.status">
								<el-radio v-for="dict in dict.type.hotel_statistics_status" :key="dict.value"
									:label="dict.value">
									{{dict.label}}
								</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="排序号:" prop="sortNumber">
							<el-input v-model="form.sortNumber" type="text" placeholder="请输入排序号"
								style="width: 200px;" />
						</el-form-item>
					</el-col>
				</el-row>


			</el-form>
			<div slot="footer" class="dialog-footer" v-if="!diLogloading">
				<el-button type="primary" :loading="btnLoading" @click="submitForm">{{form.configId?'保 存':'确 定'}}
				</el-button>
				<el-button @click="cancel" :disabled="btnLoading">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		meetingVenueAdd,
		meetingVenueList,
		meetingVenueEdit,
		meetingVenueDelete,
		meetingVenueDetails
	} from "@/api/meeting/content"
	import QuillEdit from "@/components/Editor";
	export default {
		components: {
			QuillEdit
		},
		dicts: ['hotel_statistics_status'],
		data() {
			return {
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 岗位表格数据
				postList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				dynamicTags: [],
				inputValue: "",
				inputVisible: "",
				diLogloading: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					name: '',
					affiliatedUnit: '',
					status: '',
					type: '1' //type 1会议室 2场地
				},
				// 表单参数
				form: {},
				btnLoading: false,
				// 表单校验
				rules: {
					name: [{
						required: true,
						message: "请输入会议室名称",
						trigger: "blur"
					}],
					affiliatedUnit: [{
						required: true,
						message: "请输入所属单位",
						trigger: "blur"
					}],
					content: [{
						required: true,
						message: "请输入会议室简介",
						trigger: "blur"
					}],
					sortNumber: [{
						required: true,
						message: "请输入排序号",
						trigger: "blur"
					}],
					picAddress: [{
						required: true,
						message: "请上传会议室图片",
						trigger: "blur"
					}],
					status: [{
						required: true,
						message: "请选择状态",
						trigger: "blur"
					}]
				}
			};
		},
		created() {
			this.getList();
		},
		methods: {
			showInput() {
				this.inputVisible = true;
				this.$nextTick(() => {
					this.$refs.saveTagInput.$refs.input.focus();
				});
			},
			handleInputConfirm() {
				let inputValue = this.inputValue;
				if (inputValue) {
					this.dynamicTags.push(inputValue);
				}
				this.inputVisible = false;
				this.inputValue = '';
			},
			handleClose(tag) {
				this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
			},
			/** 查询岗位列表 */
			getList() {
				this.loading = true;
				meetingVenueList(this.queryParams).then(response => {
					this.postList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
			},
			// 表单重置
			reset() {
				this.form = {
					content: undefined,
					name: undefined,
					chartAddress: undefined,
					affiliatedUnit: undefined,
					picAddress: undefined,
					sortNumber: undefined,
					f1: undefined,
					status: "0",
				};
				this.dynamicTags = [];
				this.inputValue = "";
				this.inputVisible = "";
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.configId)
				this.single = selection.length != 1
				this.multiple = !selection.length
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.open = true;
				this.title = "新增";
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				let postId = row.postId || this.ids
				if (Array.isArray(postId)) postId = postId.toString()
				this.open = true;
				this.diLogloading = true
				this.title = "编辑";
				meetingVenueDetails(postId).then(response => {
					this.form.configId = response.data.configId
					for (let i in this.form) this.form[i] = response.data[i];
					if(this.form.f1) this.dynamicTags = this.form.f1.split(",");
					if (response.data.f1) {
						this.form.f1 = response.data.f1.split(",");
					};
					this.diLogloading = false;
				});
			},
			/** 提交按钮 */
			submitForm: function() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						this.btnLoading = true;
						let form = JSON.parse(JSON.stringify(this.form));
						form.type = "1";
						form.f1 = this.dynamicTags.join(",");
						if (this.form.configId != undefined) {
							meetingVenueEdit(form).then(response => {
								this.$modal.msgSuccess("编辑成功");
								this.open = false;
								this.btnLoading = false;
								this.getList();
							});
						} else {
							meetingVenueAdd(form).then(response => {
								this.$modal.msgSuccess("新增成功");
								this.open = false;
								this.btnLoading = false;
								this.getList();
							});
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				let postIds = row.configId || this.ids;
				if (Array.isArray(postIds)) postIds = postIds.toString()
				this.$modal.confirm('此操作将要删除所选的数据，是否继续？').then(function() {
					return meetingVenueDelete(postIds);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			}
		}
	};
</script>
<style lang="scss" scope>
</style>
